package com.catazzz.springbootstudy.common.config;

import com.catazzz.springbootstudy.common.security.CustomAccessDeniedHandler;
import com.catazzz.springbootstudy.common.security.CustomAuthenticationEntryPoint;
import com.catazzz.springbootstudy.common.security.CustomerAuthenticationFailureHandler;
import com.catazzz.springbootstudy.common.security.CustomerAuthenticationSuccessHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.authentication.RememberMeServices;
import org.springframework.session.security.web.authentication.SpringSessionRememberMeServices;

/**
 * @作者: Tao
 * @日期: 2025/1/7
 * @时间: 17:16
 * @描述: 配置Spring Security的类，用于定制应用程序的安全措施 <br><pre>
 */
@Slf4j
//@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    // 用户详情服务，用于加载用户特定数据
//    @Autowired
    private UserDetailsService userDetailsService;

//    @Autowired
    private CustomerAuthenticationSuccessHandler successHandler;

//    @Autowired
    private CustomerAuthenticationFailureHandler failureHandler;

//    @Autowired
    private CustomAccessDeniedHandler deniedHandler;

//    @Autowired
    private CustomAuthenticationEntryPoint entryPoint;

    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

    /**
     * 配置并返回一个RememberMe服务实例
     *
     * RememberMe服务允许用户在登录后长时间内不需要重新认证，即使关闭浏览器后再次访问时仍能保持登录状态
     *
     * @return RememberMeServices 实例，配置了有效的持续时间
     */
    @Bean
    public RememberMeServices rememberMeServices() {
        // 创建一个SpringSessionRememberMeServices实例
        SpringSessionRememberMeServices rememberMeServices = new SpringSessionRememberMeServices();

        // 设置RememberMe cookie的有效时间为3600秒（1小时）
        rememberMeServices.setValiditySeconds(3600);

        // 返回配置好的RememberMe服务实例
        return rememberMeServices;
    }


//    @Override
//    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
//        auth.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder());
//    }

//    /**
//     * 配置HttpSecurity以定义请求的授权规则
//     *
//     * @param http 用于配置HTTP请求安全性的对象
//     * @throws Exception 配置过程中可能抛出的异常
//     */
//    @Override
//    protected void configure(HttpSecurity http) throws Exception {
//        http.authorizeRequests()
////                .antMatchers("/doc.html", "/webjars/**", "/v2/api-docs",
////                        "/swagger-resources/**", "/configuration/**",
////                        "/swagger-ui.html", "/swagger-ui/**").permitAll() // 允许对/redis/**路径的请求，不需要用户认证
//                .antMatchers("/security/config").hasAnyAuthority("config") // 需要config权限
//                .antMatchers("/security/anonymous").anonymous() // 匿名用户,无需登录
//                .antMatchers("/security/permitAll").permitAll() // 无需用户认证
//                .anyRequest().authenticated() // 所有请求 // 需要用户认证
//                .and().formLogin() // 启用表单登录
//                .successHandler(successHandler) // 设置登录成功处理器
//                .failureHandler(failureHandler) // 设置登录失败处理器
//                .and().rememberMe()
//                .rememberMeServices(rememberMeServices())
//                .and().userDetailsService(userDetailsService) // 使用自定义的用户详情服务
//                .csrf().disable(); // 关闭CSRF保护，适用于本应用的需要
//
////        http.exceptionHandling().accessDeniedHandler(deniedHandler)
////                .authenticationEntryPoint(entryPoint);
//    }
}

